<template>
	<view class="bg_F9F9F9">
		<view class="u-p-20">
			<view class="u-p-20 bg_FFFFFF u-b-r-20 u-m-b-20" v-for="(item,index) in list" :key="index">
				<view class="u-flex u-row-between" @click="selectAdd(item)">
					<view class="u-weight u-m-t-10">
						{{item.type==1?'个人发票':'企业发票'}}
					</view>
					<view class="u-weight u-m-t-10">
						{{item.name}}
					</view>
				</view>
				<view class="u-m-t-20 u-flex u-row-between u-row-center">
					<view class="">
						<!-- <view class="u-w-130 u-h-50 u-l-h-50 u-text-center bg_FC3533FF cl_FFF u-font-20 u-b-r-10"
							v-if="item.is_default==1">
							默认地址
						</view> -->
					</view>
					<view class="u-flex u-font-24">
						<view class="color_F5B927 u-m-r-50" @click="editClick(item)">
							<u-icon name="edit-pen-fill" color="#3377FF" size="40"></u-icon>
						</view>
						<view class="c_zs" @click="delClick(item)">
							<u-icon name="trash-fill" color="#FD1D4D" size="40"></u-icon>
						</view>
					</view>
				</view>
			</view>
			<view class="u-p-20">
				<u-loadmore :status="status" icon-type="flower" bg-color="transperant" margin-top="30"
					margin-bottom="30" />
			</view>
		</view>
		<view class="u-h-140">

		</view>
		<view class="u-fixed u-w-750 u-b-0 u-z-300 u-p-25 bs_EFEFEF bg_FFFFFF">
			<view class="bg_FC3533FF u-h-80 u-l-h-80 u-text-center cl_FFF u-b-r-20" @click="addClick">
				添加发票
			</view>
		</view>
		<u-modal v-model="showModal" content="确定要修改地址吗?" show-cancel-button @confirm="confirm" @cancel="cancel">
		</u-modal>
		<u-toast ref="uToast" />
	</view>
</template>
<script>
export default {
	data() {
		return {
			showModal: false,
			addId: '',
			order_sn: '',
			params: {
				page: 1,
				page_size: 20
			},
			// 加载更多参数
			status: 'loadmore',
			list: [],
			// button参数
			hairLine: true,
			ripple: true,
			customStyle: this.$btn,
			// 支付订单选择地址标识
			money: 0,
			from: ''
		}
	},
	onReady() {

	},
	onLoad(o) {
		this.money = o.money
		this.from = o.from
		this.order_sn = o.order_sn
		
	},
	onShow() {
		this.clear()
		this.add_open_list()
	},
	// 下拉到底事件
	onReachBottom() {
		//避免多次触发
		if (this.status == 'loading' || this.status == 'nomore') {
			return;
		}
		this.add_open_list()
	},
	methods: {
		// 初始化
		clear() {
			this.params.page = 1
			this.list = []
		},
		// 地址详情
		async add_open_list() {
			this.$api.add_open_list(this.params,res=>{
				if (res.data.code == 1) {
					if (res.data.data.data.length < 20) {
						this.status = "nomore"
					} else {
						this.params.page = this.params.page + 1
						this.status = "loadmore"
					}
					this.list = this.list.concat(res.data.data.data)
					uni.stopPullDownRefresh();
				}
			})
			
		},

		// 订单选择地址
		selectAdd(item) {
			let pages = getCurrentPages();
			if (this.from == "order") {
				// #ifdef MP-WEIXIN || APP-PLUS
				let currPage = pages[pages.length - 1].$vm;
				let prevPage = pages[pages.length - 2].$vm; //上一个页面
				// #endif
				// #ifdef H5
				let currPage = pages[pages.length - 1];
				let prevPage = pages[pages.length - 2]; //上一个页面
				// #endif
				prevPage.address = item
				uni.removeStorageSync('edit_address')
				uni.navigateBack({
					delta: 1
				});
			}
			if (this.from == "edit") {
				this.addId = item.id
				this.showModal = true

			}
		},

		async confirm() {
			let res = await this.$u.api.modify_order_address({ order_sn: this.order_sn, address_id: this.addId })
			const { code, msg } = res
			if (code === 1) {
				this.$refs.uToast.show({
					title: msg,
					type: 'success',
					callback: cal => {
						uni.navigateBack({
							delta: 1
						})
					}
				})
			}
		},
		cancel() {
			uni.navigateBack({
				delta: 1
			});
		},
		// 添加地址事件
		addClick() {
			this.$u.route({
				url: '/subcontract/invoice/rise/addRise',
				params: {
					type: 'add'
				}
			})
		},
		// 编辑地址事件
		editClick(item) {
			let arr={}
			if(item.type==1){
				arr={
					id:item.id,
					type:1,
					name:item.name,
					phone:item.phone || '',
					mobile:item.mobile,
					email:item.email,
					is_default:item.is_default==0?false:true,
				}
			}else{
				arr={
					id:item.id,
					name:item.name,
					type:2,
					tax_number:item.tax_number,
					bank:item.bank,
					bank_number:item.bank_number,
					address:item.address,
					mobile:item.mobile,
					is_default:item.is_default==0?false:true,
				}
			}
			uni.setStorageSync('edit_rise', item)
			this.$u.route({
				url: '/subcontract/invoice/rise/addRise',
				params: {
					type: 'edit',
				}
			})
		},
		async delClick(item) {
			var that = this
			uni.showModal({
				title: '提示',
				content: `您确定删除吗?`,
				success: async (res) => {
					if (res.confirm) {
						that.$api.do_delete({ id: item.id },res=>{
							if (res.data.code == 1) {
								that.$refs.uToast.show({
									title: res.data.msg,
									type: 'success',
									callback: cal => {
										that.clear()
										that.add_open_list()
									}
								})
							}
						})
						
					} else if (res.cancel) { }
				}
			});
		}
	}
}
</script>

<style lang="scss">
page {
	background: #F4F7F9;
}
</style>
